Skip to content

feat(hashes): add FNV-1a 32-bit and 64-bit hash algorithms - #15772

Open
IsmeetPandey wants to merge 2 commits into
TheAlgorithms:masterfrom
IsmeetPandey:feat/add-fnv1a-hash
Open

IsmeetPandey wants to merge 2 commits into
TheAlgorithms:masterfrom
IsmeetPandey:feat/add-fnv1a-hash

Conversation

@IsmeetPandey

Copy link
Copy Markdown

Describe your change

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

This pull request implements the Fowler-Noll-Vo (FNV-1a) non-cryptographic hash algorithm in hashes/fnv1a.py, providing both 32-bit (fnv1a_32) and 64-bit (fnv1a_64) variants.

Algorithm Summary

  • FNV-1a vs FNV-1: In FNV-1a, the data octet is XORed into the hash state before multiplying by the prime. This reordering provides significantly better avalanche behavior for the least significant bits of short keys.
  • Constants:
    • 32-bit:
      • Prime: 0x01000193 (16777619)
      • Offset basis: 0x811c9dc5 (2166136261)
      • Mask: 0xFFFFFFFF
    • 64-bit:
      • Prime: 0x100000001b3 (1099511628211)
      • Offset basis: 0xcbf29ce484222325 (14695981039346656037)
      • Mask: 0xFFFFFFFFFFFFFFFF
  • Complexity:
    • Time Complexity: O(n) where n is the number of bytes in data.
    • Space Complexity: O(1) auxiliary space.
  • Testing & Verification:
    • Full Python type hints (data: bytes | str -> int).
    • Supports string (UTF-8 encoded) and raw byte inputs (bytes, bytearray).
    • Strict type checking with TypeError.
    • 18 comprehensive doctests verified locally with pytest --doctest-modules hashes/fnv1a.py and python hashes/fnv1a.py -v.

Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues, then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

@algorithms-keeper algorithms-keeper Bot added the awaiting reviews This PR is ready to be reviewed label Nov 17, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant